package wowrl.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import wowrl.engine.WOWRL_Engine;

public class StatusPanel extends JPanel {
	
	private Color backgroundColor;
	
	//Store the message display
	DefaultListModel messageDisplay;
	
	//Store the game engine
	WOWRL_Engine engine;
	
	//Store the display objects
	JLabel playerHealth;
	
	//Allows us to scroll the list
	JScrollPane listScroller;
	JList list;
	
	public StatusPanel(WOWRL_Engine engine){
		// Set up the engine
		this.engine = engine;
		
		// We will manually layout
		setLayout(null);
		
		// Set the panel's size
		setSize(800,120);
		
		// Set the background color
		backgroundColor = new Color((float) 0.15, (float) 0.15, (float) 0.15);
		
		//Create the health display
		playerHealth = new JLabel();
		playerHealth.setFont(new Font("Serif", Font.BOLD, 18));
		playerHealth.setForeground(Color.WHITE);
		playerHealth.setBackground(backgroundColor);
		playerHealth.setOpaque(true);
		add(playerHealth);
		
		//Create the message display and imbed it in a JList
		messageDisplay = new DefaultListModel();
		list = new JList(messageDisplay); //data has type Object[]
		list.setCellRenderer(new CustomListRenderer());
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
	    list.setLayoutOrientation(JList.VERTICAL);
	    list.setVisibleRowCount(-1);
	    list.setEnabled(false);
	    list.setForeground(new Color((float) 0.0, (float) 0.0, (float) 0.0));
	    list.setSelectionForeground(new Color((float) 0.0, (float) 0.0, (float) 0.0));
	    
	    // Encapsulate the display in a scrollable pane
	    listScroller = new JScrollPane(list);
	    listScroller.setPreferredSize(new Dimension(635,110));
		listScroller.setBounds(0,0,160,480);
	    add(listScroller);
	   	  	    
		//Layout the display
		playerHealth.setBounds    (652,   5, 100,  25);
		listScroller.setBounds(5,5,635,110);
		refresh();
	}
	
	public void paintComponent(Graphics g) {
		g.setColor(new Color((float) 0.5, (float) 0.5, (float) 0.5));
		g.fillRect(0,0,800,120);
		
		g.setColor(backgroundColor);
		g.fillRect(5,5,635,110);
		g.fillRect(645,5,800,110);
		
	}
	public void refresh(){
		int[] x;
		x = new int[1];
		x[0] = 0;
		playerHealth.setText("HP: " + engine.getPlayerHealth() + "/" + engine.getPlayerMaxHP());				
		
		messageDisplay.clear();
		
		//Each message string gets added to the display
		for (String message : engine.getMessageLog()){
			messageDisplay.addElement(message.toString());						
		}
		
		//Refrehes the listScroller with the new information, to ensure accurate scrolling
		listScroller.validate();
		
		//Scroll to the last line
		listScroller.getVerticalScrollBar().setValue(
			listScroller.getVerticalScrollBar().getMaximum()
		);		
	}
}
